/*
 * Copyright 2019 Flipkart Internet Pvt. Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.flipkart.ohos.proteus.value;

import ohos.agp.components.element.Element;
import ohos.agp.components.element.StateElement;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;
import ohos.global.resource.solidxml.Theme;
import ohos.utils.LruBuffer;

import java.io.IOException;

/**
 * ColorResource
 *
 * @author aditya.sharat
 */
public class Resource extends Value {
    public static final String RESOURCE_PREFIX_ANIMATOR_PROPERTY = "@anim/";
    public static final String RESOURCE_PREFIX_BOOLEAN = "@bool/";
    public static final String RESOURCE_PREFIX_COLOR = "@color/";
    public static final String RESOURCE_PREFIX_DIMENSION = "@dimen/";
    public static final String RESOURCE_PREFIX_ELEMENT = "@Element/";
    public static final String RESOURCE_PREFIX_STRING = "@string/";

    public static final String ANIM = "anim";
    public static final String BOOLEAN = "bool";
    public static final String COLOR = "color";
    public static final String DIMEN = "dimen";
    public static final String ELEMENT = "Element";
    public static final String STRING = "string";

    public static final Resource NOT_FOUND = new Resource(0);

    public final int resId;
    private Element element;

    private Resource(int resId) {
        this.resId = resId;
    }

    public static boolean isAnimatorProperty(String string) {
        return string.startsWith(RESOURCE_PREFIX_ANIMATOR_PROPERTY);
    }

    public static boolean isBoolean(String string) {
        return string.startsWith(RESOURCE_PREFIX_BOOLEAN);
    }

    public static boolean isColor(String string) {
        return string.startsWith(RESOURCE_PREFIX_COLOR);
    }

    public static boolean isDimension(String string) {
        return string.startsWith(RESOURCE_PREFIX_DIMENSION);
    }

    public static boolean isElement(String string) {
        return string.startsWith(RESOURCE_PREFIX_ELEMENT);
    }

    public static boolean isString(String string) {
        return string.startsWith(RESOURCE_PREFIX_STRING);
    }

    public static boolean isResource(String string) {
        return isAnimatorProperty(string) || isBoolean(string) || isColor(string)
            || isDimension(string) || isElement(string) || isString(string);
    }

    public static Boolean getBoolean(int resId, Context context) {
        try {
            return context.getResourceManager().getElement(resId).getBoolean();
        } catch (IOException | NotExistException | WrongTypeException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Integer getColor(int resId, Context context) {
        try {
            return context.getResourceManager().getElement(resId).getColor();
        } catch (IOException | NotExistException | WrongTypeException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static StateElement getColorStateList(int resId, Context context) {
        //AttrSet attrSet = null;
        //if (attrSet.getAttr(resId).isPresent()) {
        //    return attrSet.getAttr(resId).get().getElement();
        //}
        return null;
    }

    public static Theme getElement(int resId, Context context) {
        return context.getTheme();
    }

    public static Float getDimension(int resId, Context context) {
        try {
            return context.getResourceManager().getElement(resId).getFloat();
        } catch (IOException | NotExistException | WrongTypeException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String getString(int resId, Context context) {
        try {
            return context.getResourceManager().getElement(resId).getString();
        } catch (IOException | NotExistException | WrongTypeException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Resource valueOf(String value, String type, Context context) {
        //if (null == value) {
        //    return null;
        //}
        //Resource resource = ResourceCache.cache.get(value);
        //if (null == resource) {
        //    int resId = attrSet.getAttr(value).get().getIntegerValue();
        //    resource = resId == 0 ? NOT_FOUND : new Resource(resId);
        //    ResourceCache.cache.put(value, resource);
        //}
        //return NOT_FOUND == resource ? null : resource;
        return null;
    }

    public static Resource valueOf(int resId) {
        return new Resource(resId);
    }

    public Boolean getBoolean(Context context) {
        return getBoolean(resId, context);
    }

    public Integer getColor(Context context) {
        return getColor(resId, context);
    }

    public Element getColorStateList(Context context) {
        return getColorStateList(resId, context);
    }

    public Theme getElement(Context context) {
        return getElement(resId, context);
    }

    public Float getDimension(Context context) {
        return getDimension(resId, context);
    }

    public Integer getInteger(Context context) {
        return getInteger(resId, context);
    }

    public String getString(Context context) {
        return getString(resId, context);
    }

    public Integer getInteger(int resId, Context context) {
        try {
            return context.getResourceManager().getElement(resId).getInteger();
        } catch (IOException | NotExistException | WrongTypeException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Value copy() {
        return this;
    }

    private static class ResourceCache {
        static final LruBuffer<String, Resource> cache = new LruBuffer<>(64);
    }
}
